package cn.tedu.csmall.product.web;

import cn.tedu.csmall.product.ex.ServiceException;
import lombok.Data;
import springfox.documentation.spring.web.json.Json;

import java.io.Serializable;

/**
 * 同意的响应结果类型
 * @author stephen
 * @version 0.0.1
 */
@Data
public class JsonResult<T> implements Serializable {

    /**
     * 业务状态码
     */
    private Integer state;
    /**
     * 发生异常时的提示消息
     */
    private String message;
    /**
     * 查询到的数据
     */
    private T data;

    public static JsonResult ok(){
       return ok(null);
    }

    public static <T> JsonResult<T> ok(T data){
        JsonResult jsonResult = new JsonResult();
        jsonResult.setState(ServiceCode.OK);
        jsonResult.setData(data);
        return jsonResult;
    }

    public static JsonResult fail(ServiceException e){
        return fail(e.getState(),e.getMessage());
    }

    public static JsonResult fail(Integer state, String message){
        JsonResult jsonResult = new JsonResult();
        jsonResult.setState(state);
        jsonResult.setMessage(message);
        return jsonResult;
    }
}
